/**
*  
* @Function TODO 
* @ProjectName credit-data-platform 
* @PackageName com.py.credit.helper    
* @FileName SendSysMsgHelper.java    
* @Date 2018年5月16日下午12:02:32
* @Author Min DongXv
* @Copyright (c) 2018, 金电联行 All Rights Reserved.   
*   
*/  
package com.py.credit.helper;

import java.io.IOException;
import java.util.concurrent.Future;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.py.credit.constant.ApiPath;
import com.py.framework.core.exception.ServiceException;
import com.py.framework.core.filter.SecurityCheckFilter;
import com.py.framework.core.http.HttpAsyncAgent;
import com.py.framework.core.log.ExceptionLogger;
import com.py.framework.core.log.OutInterfaceLogger;
import com.py.framework.core.log.SystemLogger;
import com.py.framework.core.rest.support.RestServiceResult;

/**
 * 
 * @Function TODO
 * @ClassName SendSysMsgHelper
 * @Date 2018年5月16日 下午12:02:32
 * @Author Min DongXv
 * 
 */
public class SendSysMsgHelper {

	/** 默认字符集  **/
	public static final String CHARSET_UTF_8 = "utf-8";
	
	public static Future<HttpResponse> postSysMsg(final String jdToken, final String baseUrl, final String jsonStr) {
		if (StringUtils.isBlank(baseUrl)) {  
            SystemLogger.info("we don't have base url, check config !");  
            throw new ServiceException("we don't have base url, check config !");  
        }  
        CloseableHttpAsyncClient hc = HttpAsyncAgent.getInstance().getAsyncHttpClient();
        hc.start();  
		HttpPost httpPost = new HttpPost(baseUrl + ApiPath.SEND_SYS_MSG);

        httpPost.setHeader("Connection","close");
        if (StringUtils.isNotBlank(jdToken)) {
        	httpPost.setHeader(SecurityCheckFilter.Token, jdToken);
        }
        
        if (StringUtils.isNotBlank(jsonStr)) {
        	StringEntity entity = new StringEntity(jsonStr, CHARSET_UTF_8);  
            entity.setContentEncoding(CHARSET_UTF_8);  
            entity.setContentType("application/json");  
            httpPost.setEntity(entity);  
        }
		return hc.execute(httpPost, new FutureCallback<HttpResponse>() {

			@Override
			public void completed(HttpResponse result) {
				try {
					String data = EntityUtils.toString(result.getEntity());
					if (StringUtils.isNotBlank(data)) {
						RestServiceResult<Object> rs = JSON.parseObject(data,
								new TypeReference<RestServiceResult<Object>>() {
								});
						if (null != rs) {
							if (rs.getCode() == RestServiceResult.CODE_SUCCESS) {
								OutInterfaceLogger.info("postSysMsg success[jdToken=" + jdToken + "]", baseUrl, null,
										jsonStr, null, data);
							} else {
								OutInterfaceLogger.info("postSysMsg failed[jdToken=" + jdToken + "]", baseUrl, null,
										jsonStr, null, data);
							}
						}
					}
				} catch (ParseException e) {
					ExceptionLogger.error(e);
				} catch (IOException e) {
					ExceptionLogger.error(e);
				}
			}

			@Override
			public void failed(Exception ex) {
				ExceptionLogger.error("",
						"postSysMsg error[" + baseUrl + "], and token[" + jdToken + "], params[" + jsonStr + "]", ex);
			}

			@Override
			public void cancelled() {
				// TODO Auto-generated method stub
			}
		});
	}
}
